/*
 * $RCSfile: NameFilter.java,v $
 * $Revision: 32.8 $
 *
 * Comments:
 *
 * (C) Copyright Biuro-Technika 1996.  All rights reserved.
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Biuro-Technika
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 *
 * $Author: michal $          $Locker:  $
 * $Date: 2009/09/22 12:09:10 $
 * $Log: NameFilter.java,v $
 * Revision 32.8  2009/09/22 12:09:10  michal
 * ParaSoft -> Biuro-Technika
 *
 * Revision 32.7  2007/09/18 11:39:05  muzzy
 * uppercase dla filterID
 *
 * Revision 32.6  2005/09/29 11:19:47  rudi
 * nowe funkcje initialize i isFilterChenged, sluzace do wyszukiwania klientow po identyfikatorze i nazwie
 *
 * Revision 32.5  2005/09/15 09:59:52  rudi
 * wyszukiwanie kontrahenta po ID i NIP
 *
 * Revision 32.4  2004/05/21 09:14:39  limbur
 * Dodano funkcje isSubstring(), getFilterString()
 *
 * Revision 32.3  2004/01/05 17:01:53  limbur
 * FIltr moze byc case sensitive/insensitive, dodano funkcje isFilterChanged(String)
 *
 * Revision 32.2  2004/01/05 09:54:13  limbur
 * Dodano funkcje getFilter()
 *
 * Revision 32.1  2003/09/01 10:51:53  limbur
 * Original source code.
 *
 */

package parasoft.centrap.utils;

public class NameFilter
{
  /*
    private String sFilter;
    private String sFilterNIP;
    private String sFilterID;
*/
    public String sFilter;
    public String sFilterNIP;
    public String sFilterID;

    private boolean substring;
    private boolean substringID;
    private boolean substringNIP;
    private boolean uppercase;

    public NameFilter(String filter)
    {
        initialize(filter, true);
    }

    public NameFilter(String filter, boolean case_sensitive)
    {
        initialize(filter, !case_sensitive);
    }

    public NameFilter(String filter, String filterID, String filterNIP) //kontrahenci
       {
           initialize(filter, filterID, filterNIP, true);
       }

    public NameFilter(String filter, String filterID)  //DbKlienci
         {
             initialize(filter, filterID, true);
         }

    public NameFilter(String filter, String filterID, boolean case_sensitive) //DbKlienci
         {
             initialize(filter, filterID, !case_sensitive);
         }


    public NameFilter(String filter, String filterID, String filterNIP, boolean case_sensitive) //kontrahenci
       {
           initialize(filter, filterID, filterNIP, !case_sensitive);
       }

    public String getFilter()
    {
        if(substring) {
            return STAR + sFilter;
        } else {
            return sFilter;
        }
    }

    public String getFilterID()
    {
        if(substringID) {
            return STAR + sFilterID;
        } else {
            return sFilterID;
        }
    }

    public String getFilterNIP()
    {
        if(substringNIP) {
            return STAR + sFilterNIP;
        } else {
            return sFilterNIP;
        }
    }


    public boolean isSubstring()
    {
        return substring;
    }

    public String getFilterString()
    {
        return sFilter;
    }

    public void setFilter(String filter, String filterID, String filterNIP)
    {
        initialize(filter, filterID, filterNIP, uppercase);
    }

    public void setFilter(String filter)
        {
            initialize(filter, uppercase);
        }

    public void setFilter(String filter, String filterID)
    {
        initialize(filter, filterID, uppercase);
    }

    public boolean isActive()
    {
        return (sFilter != null || sFilterNIP != null || sFilterID != null);
    }

    public boolean isFiltered(String value)
    {
        if (sFilter == null) {
            return true;
        }

        if(value == null) {
            return false;
        }
        if(!isActive()) {
            return true;
        }
        if(uppercase) {
            value = value.toUpperCase();
        }
        if(substring) {
            if(value.indexOf(sFilter) >= 0) {
                return true;
            }
        }
        else if(value.startsWith(sFilter)) {
            return true;
        }
        return false;
    }

    public boolean isFilteredID(String value)
    {
        if (sFilterID == null) {
            return true;
        }

        if(value == null) {
            return false;
        }
        if(!isActive()) {
            return true;
        }
        if(uppercase) {
            value = value.toUpperCase();
        }
        if(substring) {
            if(value.indexOf(sFilterID) >= 0) {
                return true;
            }
        }
        else if(value.startsWith(sFilterID)) {
            return true;
        }

        return false;
    }

    public boolean isFilteredNIP(String value)
    {
        if (sFilterNIP == null) {
            return true;
        }

        if(value == null) {
            return false;
        }
        if(!isActive()) {
            return true;
        }
        if(uppercase) {
            value = value.toUpperCase();
        }
        if(substring) {
            if(value.indexOf(sFilterNIP) >= 0) {
                return true;
            }
        }
        else if(value.startsWith(sFilterNIP)) {
            return true;
        }

        return false;
    }


    public boolean isFilterChanged(String filter, String filterID, String filterNIP)
    {
        if(filter != null) {
            if(uppercase) {
                filter = filter.trim().toUpperCase();
            }
            else {
                filter = filter.trim();
            }
            if(sFilter == null) {
                if(filter.length() > 0) {
                    return true;
                }
            }
            else {
                if(!sFilter.equals(filter)) {
                    return true;
                }
            }
        }

        if(filterID != null) {
            if (uppercase) {
                filterID = filterID.trim().toUpperCase();
            } else {
                filterID = filterID.trim();
            }
            if(sFilterID == null) {
                if(filterID.length() > 0) {
                    return true;
                }
            }
            else {
                if(!sFilterID.equals(filterID)) {
                    return true;
                }
            }
        }

        if(filterNIP != null) {
            filterNIP = filterNIP.trim();
            if(sFilterNIP == null) {
                if(filterNIP.length() > 0) {
                    return true;
                }
            }
            else {
                if(!sFilterNIP.equals(filterNIP)) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isFilterChanged(String filter)
       {
           if(filter != null) {
               if(uppercase) {
                   filter = filter.trim().toUpperCase();
               }
               else {
                   filter = filter.trim();
               }
               if(sFilter == null) {
                   if(filter.length() > 0) {
                       return true;
                   }
               }
               else {
                   if(!sFilter.equals(filter)) {
                       return true;
                   }
               }
           }
           return false;
       }

       public boolean isFilterChanged(String filter, String filterID)
       {
           if(filter != null) {
               if(uppercase) {
                   filter = filter.trim().toUpperCase();
               }
               else {
                   filter = filter.trim();
               }
               if(sFilter == null) {
                   if(filter.length() > 0) {
                       return true;
                   }
               }
               else {
                   if(!sFilter.equals(filter)) {
                       return true;
                   }
               }
           }

           if(filterID != null) {
               if(uppercase) {
                   filterID = filterID.trim().toUpperCase();
               }
               else {
                   filterID = filter.trim();
               }
               if(sFilterID == null) {
                   if(filterID.length() > 0) {
                       return true;
                   }
               }
               else {
                   if(!sFilterID.equals(filterID)) {
                       return true;
                   }
               }
           }

           return false;
       }


    private void initialize(String filter, boolean uppercase)
        {
            this.uppercase = uppercase;
            this.substring = false;
            if(filter != null) {
                if(uppercase) {
                    sFilter = filter.trim().toUpperCase();
                } else {
                    sFilter = filter.trim();
                }
                if(sFilter.length() == 0) {
                    sFilter = null;
                } else {
                    if((sFilter.length() > 1) && (sFilter.charAt(0) == STAR)) {
                        sFilter = sFilter.substring(1);
                        substring = true;
                    }
                }
            } else {
                sFilter = null;
            }
        }


    private void initialize(String filter, String filterID, boolean uppercase)
    {
        this.uppercase = uppercase;
        this.substring = false;
        this.substringID = false;

        if(filter != null) {
            if(uppercase) {
                sFilter = filter.trim().toUpperCase();
            } else {
                sFilter = filter.trim();
            }
            if(sFilter.length() == 0) {
                sFilter = null;
            } else {
                if((sFilter.length() > 1) && (sFilter.charAt(0) == STAR)) {
                    sFilter = sFilter.substring(1);
                    substring = true;
                }
            }
        } else {
            sFilter = null;
        }

        if(filterID != null) {
            sFilterID = filterID.trim();
            if (sFilterID.length() == 0) {
                sFilterID = null;
            }
            else {
                if ( (sFilterID.length() > 1) && (sFilterID.charAt(0) == STAR)) {
                    sFilterID = sFilterID.substring(1);
                    substringID = true;
                }
            }
        }

        else {
            sFilterID = null;
        }

    }


    private void initialize(String filter, String filterID, String filterNIP, boolean uppercase)
    {
        this.uppercase = uppercase;
        this.substring = false;
        this.substringID = false;
        this.substringNIP = false;

        if(filter != null) {
            if(uppercase) {
                sFilter = filter.trim().toUpperCase();
            } else {
                sFilter = filter.trim();
            }
            if(sFilter.length() == 0) {
                sFilter = null;
            } else {
                if((sFilter.length() > 1) && (sFilter.charAt(0) == STAR)) {
                    sFilter = sFilter.substring(1);
                    substring = true;
                }
            }
        } else {
            sFilter = null;
        }

        if(filterID != null) {
            if(uppercase) {
                sFilterID = filterID.trim().toUpperCase();
            } else {
                sFilterID = filterID.trim();
            }
            if (sFilterID.length() == 0) {
                sFilterID = null;
            }
            else {
                if ( (sFilterID.length() > 1) && (sFilterID.charAt(0) == STAR)) {
                    sFilterID = sFilterID.substring(1);
                    substringID = true;
                }
            }
         }

         else {
            sFilterID = null;
        }

        if(filterNIP != null) {
            sFilterNIP = filterNIP.trim();
            if (sFilterNIP.length() == 0) {
                sFilterNIP = null;
            }
            else {
                if ( (sFilterNIP.length() > 1) && (sFilterNIP.charAt(0) == STAR)) {
                    sFilterNIP = sFilterNIP.substring(1);
                    substringNIP = true;
                }
            }
        }
         else {
            sFilterNIP = null;
        }

    }

    private final static char STAR = '*';
}
